package edu.iul.daam.rap.pages.db;

import java.util.LinkedList;
import java.util.Random;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

import edu.iul.daam.rap.pages.Areas;
import edu.iul.daam.rap.pages.Page;

public class PagesDB extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "rap-pages.db";
	private static final int DATABASE_VERSION = 1;

	public static final String TABLE_NAME = "pages";

	public static final String _ID = BaseColumns._ID;
	public static final String URL = "url";
	public static final String AREA = "area";
	public static final String TEXT = "text";
	public static final String AUDIO = "audio";
	public static final String VIDEO = "video";
	public static final String IMAGE = "image";
	public static final String POINTS = "points";

	public PagesDB(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}


	public void insert(String url, Areas area, boolean text, boolean audio, boolean video, 
			boolean image, int points) {
		SQLiteDatabase db = getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(URL, url);
		values.put(AREA, convertAreasToString(area));
		values.put(TEXT, convertBooleanToInteger(text));
		values.put(AUDIO, convertBooleanToInteger(audio));
		values.put(VIDEO, convertBooleanToInteger(video));
		values.put(IMAGE, convertBooleanToInteger(image));
		values.put(POINTS, points);

		db.insertOrThrow(TABLE_NAME, null, values);
	}

	public Cursor getAll(Activity activity) {
		String[] from = { _ID, URL, AREA, TEXT, AUDIO, VIDEO, IMAGE, POINTS};
		String order = URL;

		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = db.query(TABLE_NAME, from, null, null, null, null, order);
		activity.getLoaderManager();
		return cursor;
	}

	public long size() {
		SQLiteDatabase db = getReadableDatabase();
		return DatabaseUtils.queryNumEntries(db, TABLE_NAME);
	}	

	public Page getPage(Activity activity){

		String[] from = { URL, AREA, TEXT, AUDIO, VIDEO, IMAGE, POINTS};
		String order = POINTS + " DESC " ;
		String limit = "3";

		Page page_chosen = new Page();

		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = db.query(TABLE_NAME, from, null, null, null, null, order, limit);
		activity.getLoaderManager();

		cursor.move((new Random().nextInt(Integer.parseInt(limit))));
		page_chosen = new Page(cursor.getString(cursor.getColumnIndex(URL)), 
				convertStringToAreas(cursor.getString(cursor.getColumnIndex(AREA))), 
				convertIntToBoolean(cursor.getInt(cursor.getColumnIndex(TEXT))), 
				convertIntToBoolean(cursor.getInt(cursor.getColumnIndex(AUDIO))),
				convertIntToBoolean(cursor.getInt(cursor.getColumnIndex(VIDEO))),
				convertIntToBoolean(cursor.getInt(cursor.getColumnIndex(IMAGE))),
				cursor.getInt(cursor.getColumnIndex(POINTS)));

		return page_chosen;
	}

	public void updatePoints(String url, int points){

		ContentValues dataToInsert = new ContentValues();                          
		dataToInsert.put("POINTS", points);
		String where = "url" + "=" + url;

		SQLiteDatabase db = getReadableDatabase();
		db.update(DATABASE_NAME, dataToInsert, where, null);
	}

	private Areas convertStringToAreas(String area) {
		for(Areas a: Areas.values())
			if (area.equals(a.name()))
				return a;
		return null;
	}

	private boolean convertIntToBoolean(int integer) {
		return integer==1;
	}

	private String convertAreasToString(Areas area) {
		return area.name();
	}

	private int convertBooleanToInteger(boolean boolean_value) {
		if(boolean_value)
			return 1;
		return 0;
	}


	public void init(){
		insert(new String("http://www.portugaldailyview.com/category/03-sport/feed"), Areas.SPORTS, true, false, false, false, 0);
		insert(new String("http://feeds.reuters.com/reuters/technologyNews?format=xml"), Areas.TECNOLOGY, true, false, false, false, 0);
		insert(new String("http://www.music-news.com/rss/news.asp"), Areas.MUSIC, true, false, false, false, 0);
		insert(new String("http://mf.feeds.reuters.com/reuters/UKScienceNews?format=xml"), Areas.SCIENCE, true, false, false, false, 0);
		insert(new String("http://mf.feeds.reuters.com/reuters/UKHealthNews?format=xml"), Areas.HEALTH, true, false, false, false, 0);
		insert(new String("http://www.portugaldailyview.com/category/01-economy/feed"), Areas.ECONOMICS, true, false, false, false, 0);
		insert(new String("http://www.portugaldailyview.com/category/05-culture/feed"), Areas.CULTURE, true, false, false, false, 0);
		insert(new String("http://www.portugaldailyview.com/category/02-politics/feed"), Areas.POLITICS, true, false, false, false, 0);
		insert(new String("http://mf.feeds.reuters.com/reuters/UKEntertainment?format=xml"), Areas.ENTERTAINMENT, true, false, false, false, 0);
		insert(new String("http://www.portugaldailyview.com/feed"), Areas.NATIONAL_NEWS, true, false, false, false, 0);
		insert(new String("http://mf.feeds.reuters.com/reuters/UKWorldNews?format=xml"), Areas.INTERNATIONAL_NEWS, true, false, false, false, 0);
		
		insert(new String("http://api.flickr.com/services/feeds/photos_public.gne?tags=sports&format=rss_200"), Areas.SPORTS, false, false, false, true, 0);
		insert(new String("http://api.flickr.com/services/feeds/photos_public.gne?tags=tecnology&format=rss_200"), Areas.TECNOLOGY, false, false, false, true, 0);
		insert(new String("http://api.flickr.com/services/feeds/photos_public.gne?tags=music&format=rss_200"), Areas.MUSIC, false, false, false, true, 0);
		insert(new String("http://api.flickr.com/services/feeds/photos_public.gne?tags=science&format=rss_200"), Areas.SCIENCE, false, false, false, true, 0);
		insert(new String("http://api.flickr.com/services/feeds/photos_public.gne?tags=health&format=rss_200"), Areas.HEALTH, false, false, false, true, 0);
		insert(new String("http://api.flickr.com/services/feeds/photos_public.gne?tags=economy&format=rss_200"), Areas.ECONOMICS, false, false, false, true, 0);
		insert(new String("http://api.flickr.com/services/feeds/photos_public.gne?tags=culture&format=rss_200"), Areas.CULTURE, false, false, false, true, 0);
		insert(new String("http://api.flickr.com/services/feeds/photos_public.gne?tags=politics&format=rss_200"), Areas.POLITICS, false, false, false, true, 0);
		insert(new String("http://api.flickr.com/services/feeds/photos_public.gne?tags=entertainment&format=rss_200"), Areas.ENTERTAINMENT, false, false, false, true, 0);
		insert(new String("http://api.flickr.com/services/feeds/photos_public.gne?tags=news&format=rss_200"), Areas.NATIONAL_NEWS, false, false, false, true, 0);
		insert(new String("http://api.flickr.com/services/feeds/photos_public.gne?tags=world&format=rss_200"), Areas.INTERNATIONAL_NEWS, false, false, false, true, 0);
	}


	@Override
	public void onCreate(SQLiteDatabase db) {
		// CREATE TABLE pages (id INTEGER PRIMARY KEY AUTOINCREMENT, url TEXT NOT NULL, area TEXT NOT NULL, text BOOLEAN, audio BOOLEAN, video BOOLEAN,image BOOLEAN,);
		String sql = "CREATE TABLE " + TABLE_NAME + " ("
				+ _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
				+ URL + " TEXT NOT NULL, "
				+ AREA + " TEXT NOT NULL, "
				+ TEXT + " BOOLEAN, "
				+ AUDIO + " BOOLEAN, "
				+ VIDEO + " BOOLEAN, "
				+ IMAGE + " BOOLEAN, "
				+ POINTS + " INTEGER"
				+ ");";

		db.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		onCreate(db);
	}

}